<template>
  <div class="upload-container">
    <el-upload
      v-if="temurl.length<1"
      :data="dataObj"
      :multiple="false"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-success="handleImageSuccess"
      class="image-uploader"
      drag
      :action="baseurl"
      accept=".jpg,.jpeg,.png,.bmp"
    >
      <i class="el-icon-upload"/>
      <div class="el-upload__text">
        将文件拖到此处，或<em>点击上传</em>
      </div>
    </el-upload>
    <div class="image-preview image-app-preview">
      <div v-show="temurl.length>1" class="image-preview-wrapper">
        <img :src="temurl">
        <div class="image-preview-action">
          <i class="el-icon-delete" @click="rmImage"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  name: 'SingleImageUpload3',
  props: ['typ'],
  data() {
    return {
      baseurl: process.env.VUE_APP_BASE_URL + "/admin/upload/index",
      temurl: '',
      dataObj: {token: '', fileType: this.typ}
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  methods: {
    rmImage() {
      this.emitInput('')
      this.temurl = ''
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    handleImageSuccess(res) {
      if (res.code == 200) {
        this.temurl = process.env.VUE_APP_BASE_URL + '/admin/Upload/previewTemp?tempPath=' + res.data.tempPath
        this.emitInput(res.data)
      } else {
        this.$message.error(res.msg);
      }
    },
    beforeUpload(file) {
      console.log(file.size)
      const isLt2M = file.size /1024/1024  < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10M')
        return false
      }
      this.dataObj.token = this.token
    },
    resetTempUrl() {
      this.temurl = ''
    },

  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.upload-container {
  position: relative;
  width: 100%;
}

.upload-container {
  position: relative;
  width: 100%;
  height: 180px;
  @include clearfix;

  .image-uploader {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 3;
    width: 100%;
    height: 180px;
  }

  .image-preview {
    position: relative;
    width: 100%;
    height: 180px;

    .image-preview-wrapper {
      position: relative;
      width: 100%;
      height: 100%;

      img {
        width: 100%;
        height: 100%;
      }
    }

    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 20px;
      background-color: rgba(0, 0, 0, .5);
      transition: opacity .3s;
      cursor: pointer;
      text-align: center;
      line-height: 180px;

      .el-icon-delete {
        font-size: 36px;
      }
    }

    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }

  .image-app-preview {
    width: auto;
    height: 180px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;

    .app-fake-conver {
      height: 44px;
      position: absolute;
      width: 100%; // background: rgba(0, 0, 0, .1);
      text-align: center;
      line-height: 64px;
      color: #fff;
    }
  }
}
</style>
